﻿// for inline in page pager
function prepareAjaxPager() {
	// add loading, get list, populate
	var $pagehandle = this.find(".pagehandle"),
		$pagetarget = this.find(".pagetarget"),
		$morelink = this.find("a.more"),
		$count = $morelink.find("span"),
		count = parseInt($count.text(), 10),
		pagesize = parseInt(this.attr("data-pagesize"), 10);

	if (isNaN(pagesize)) pagesize = 10;

	count -= pagesize;
	(count > 0) ? $count.text(count) : $pagehandle.hide();


	$morelink.click(function (e) {
		e.preventDefault();

		$morelink.addClass("loadings");

		$.get($morelink.attr("href"), { caceh: 100 }, function (data) {

			if ($.trim(data).length > 0) {

				$d = $(data).appendTo($pagetarget);

				// passback the returned link from response layer (might cause an issue if not "find")
				var response = $d.filter(".response").text();
				if (!response) response = $d.find(".response").text();
				($.trim(response) != '') ? $morelink.attr("href", response) : $pagehandle.hide();

				// update count
				count -= pagesize;
				(count > 0) ? $count.text(count) : $pagehandle.hide();

				// rebind anything that needs rebinding, context is parent of pagetarget
				rewireload($pagetarget.parent());

			}

			$morelink.removeClass("loadings");

		});
	});


}